<?php
// +----------------------------------------------------------------------
// | ThinkSNS
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.thinksns.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Daniel Yang <desheng.young@gmail.com>
// +----------------------------------------------------------------------
//

/**
 +------------------------------------------------------------------------------
 * RssParseService RSS解析服务
 * 实现对RSS格式内容的解析
 * 为博客等应用的RSS导入提供支持
 +------------------------------------------------------------------------------
 * @category	addons
 * @package		addons
 * @subpackage  services
 * @author		Daniel Yang <desheng.young@gmail.com>
 * @version		$Id$
 +------------------------------------------------------------------------------
 */

class RssParseService extends Service {
	private $cache_location = '';
	private $cache_duration = '';

	public function __construct() {
		$this->init();
	}
	
	public function init() {
		include_once(SITE_PATH.'/addons/libs/RSS/SimplePie/simplepie.inc.php');
		$this->cache_location = RUNTIME_PATH . '/rss';
		$this->cache_duration = 3600;
	}

	
	
	//解析XML
	public function parse($url, $use_cache = true) {
		$feed = new SimplePie();
		$res  = array('error'=>false, 'info'=>'Everything OK!');

		//缓存
		if($use_cache != false) {
			!is_writable($this->cache_location) && mkdir($this->cache_location);
			$feed->set_cache_location($this->cache_location);
			$feed->set_cache_duration($this->cache_duration);
		}

		//SimplePie
		$feed->set_feed_url($url);
		if(!$feed->init()) {
			return array('error'=>true, 'info'=>'Failed to get content.');
		}
		$feed->handle_content_type();

		//channel
		$res['title']		= $feed->get_title();
		$res['link']		= $feed->get_link();
		$res['description'] = $feed->get_description();
		if(!$res['favicon'] = $feed->get_favicon()) {
			$res['favicon'] = '替换图标';
		}

		//items
		foreach($feed->get_items() as $v) {
			$item = array();

			$v->get_permalink() && $item['link'] = $v->get_permalink();
			$item['title']   = $v->get_title();
			$item['date']    = strtotime($v->get_date('Y-m-d H:i:s'));
			$item['content'] = $v->get_content();
			// TODO : 在页面内播放视频/音频
			if ($enclosure = $v->get_enclosure(0)) {
				$item['enclosure']['type'] = $enclosure->get_type();
				$item['enclosure']['size'] = $enclosure->get_size(); //MB
				$item['enclosure']['link'] = $enclosure->get_link();
				$enclosure->get_thumbnail() && $item['enclosure']['thumbnail'] = $enclosure->get_thumbnail();
			}

			$res['items'][] = $item;
		}

		return $res;
	}



	// setter & getter
	public function getCacheLocation() {
		return $this->cache_location;
	}
	public function setCacheLocation($location) {
		$this->cache_location = $location;
	}
	public function getCacheDuration() {
		return $this->cache_duration;
	}
	public function setCacheDuration($duration) {
		$this->cache_duration = $duration;
	}



	public function run() {
		
	}
	
	public function _start() {
		
	}
	
	public function _stop() {
		
	}
	
	public function _install() {
		
	}
	
	public function _uninstall() {
		
	}
}
?>